home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / Elf.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-16  |  7.8 KB  |  329 lines

  1. /* Copyright (C) 1986-2001 by Digital Mars. $Revision: 1.1.1.1 $ */
  2. #if __SC__ || __RCC__
  3. #pragma once
  4. #endif
  5.  
  6. #ifndef __ELF_H
  7. #define __ELF_H 1
  8.  
  9. #ifdef    __cplusplus
  10. extern "C" {
  11. #endif
  12.  
  13. #pragma pack(1)
  14.  
  15. typedef unsigned long    Elf32_Addr;
  16. typedef unsigned short    Elf32_Half;
  17. typedef unsigned long    Elf32_Off;
  18. typedef long        Elf32_Sword;
  19. typedef unsigned long    Elf32_Word;
  20.  
  21. #define    ELF32_FSZ_ADDR    4
  22. #define    ELF32_FSZ_HALF    2
  23. #define    ELF32_FSZ_OFF    4
  24. #define    ELF32_FSZ_SWORD    4
  25. #define    ELF32_FSZ_WORD    4
  26.  
  27. /* ELF header */
  28.  
  29. #define    EI_NIDENT    16
  30.  
  31. typedef struct {
  32.     unsigned char    e_ident[EI_NIDENT];    /* ident bytes */
  33.     Elf32_Half    e_type;            /* file type */
  34.     Elf32_Half    e_machine;        /* target machine */
  35.     Elf32_Word    e_version;        /* file version */
  36.     Elf32_Addr    e_entry;        /* start address */
  37.     Elf32_Off    e_phoff;        /* phdr file offset */
  38.     Elf32_Off    e_shoff;        /* shdr file offset */
  39.     Elf32_Word    e_flags;        /* file flags */
  40.     Elf32_Half    e_ehsize;        /* sizeof ehdr */
  41.     Elf32_Half    e_phentsize;        /* sizeof phdr */
  42.     Elf32_Half    e_phnum;        /* number phdrs */
  43.     Elf32_Half    e_shentsize;        /* sizeof shdr */
  44.     Elf32_Half    e_shnum;        /* number shdrs */
  45.     Elf32_Half    e_shstrndx;        /* shdr string index */
  46. } Elf32_Ehdr;
  47.  
  48. #define    EI_MAG0        0        /* e_ident[] indexes */
  49. #define    EI_MAG1        1
  50. #define    EI_MAG2        2
  51. #define    EI_MAG3        3
  52. #define    EI_CLASS    4
  53. #define    EI_DATA        5
  54. #define    EI_VERSION    6
  55. #define    EI_PAD        7
  56.  
  57. #define    ELFMAG0        0x7f        /* EI_MAG */
  58. #define    ELFMAG1        'E'
  59. #define    ELFMAG2        'L'
  60. #define    ELFMAG3        'F'
  61. #define    ELFMAG        "\177ELF"
  62. #define    SELFMAG        4
  63.  
  64. // Where x = e_ident
  65.  
  66. #define ISELF(x) (((x[EI_MAG0])==ELFMAG0) &&    \
  67.           ((x[EI_MAG1])==ELFMAG1) &&    \
  68.                   ((x[EI_MAG2])==ELFMAG2) &&    \
  69.           ((x[EI_MAG3])==ELFMAG3)) 
  70.  
  71. #define    ELFCLASSNONE    0        /* EI_CLASS */
  72. #define    ELFCLASS32    1
  73. #define    ELFCLASS64    2
  74. #define    ELFCLASSNUM    3
  75.  
  76. #define    ELFDATANONE    0        /* EI_DATA */
  77. #define    ELFDATA2LSB    1
  78. #define    ELFDATA2MSB    2
  79. #define    ELFDATANUM    3
  80.  
  81. #define    ET_NONE        0        /* e_type */
  82. #define    ET_REL        1
  83. #define    ET_EXEC        2
  84. #define    ET_DYN        3
  85. #define    ET_CORE        4
  86. #define    ET_NUM        5
  87.  
  88. #define    ET_LOPROC    0xff00        /* processor specific range */
  89. #define    ET_HIPROC    0xffff
  90.  
  91. #define    EM_NONE        0        /* e_machine */
  92. #define    EM_M32        1        /* AT&T WE 32100 */
  93. #define    EM_SPARC    2        /* Sun SPARC */
  94. #define    EM_386        3        /* Intel 80386 */
  95. #define    EM_68K        4        /* Motorola 68000 */
  96. #define    EM_88K        5        /* Motorola 88000 */
  97. #define    EM_486        6        /* Intel 80486 */
  98. #define    EM_860        7        /* Intel i860 */
  99. #define    EM_NUM        8
  100.  
  101. #define    EV_NONE        0        /* e_version, EI_VERSION */
  102. #define    EV_CURRENT    1
  103. #define    EV_NUM        2
  104.  
  105. /* Program header */
  106.  
  107. typedef struct {
  108.     Elf32_Word    p_type;        /* entry type */
  109.     Elf32_Off    p_offset;    /* file offset */
  110.     Elf32_Addr    p_vaddr;    /* virtual address */
  111.     Elf32_Addr    p_paddr;    /* physical address */
  112.     Elf32_Word    p_filesz;    /* file size */
  113.     Elf32_Word    p_memsz;    /* memory size */
  114.     Elf32_Word    p_flags;    /* entry flags */
  115.     Elf32_Word    p_align;    /* memory/file alignment */
  116. } Elf32_Phdr;
  117.  
  118. #define    PT_NULL        0        /* p_type */
  119. #define    PT_LOAD        1
  120. #define    PT_DYNAMIC    2
  121. #define    PT_INTERP    3
  122. #define    PT_NOTE        4
  123. #define    PT_SHLIB    5
  124. #define    PT_PHDR        6
  125. #define    PT_NUM        7
  126.  
  127. #define    PT_LOPROC    0x70000000    /* processor specific range */
  128. #define    PT_HIPROC    0x7fffffff
  129.  
  130. #define    PF_R        0x4        /* p_flags */
  131. #define    PF_W        0x2
  132. #define    PF_X        0x1
  133.  
  134. #define    PF_MASKPROC    0xf0000000    /* processor specific values */
  135.  
  136. /* Section header */
  137.  
  138. typedef struct {
  139.     Elf32_Word    sh_name;    /* section name */
  140.     Elf32_Word    sh_type;    /* SHT_... */
  141.     Elf32_Word    sh_flags;    /* SHF_... */
  142.     Elf32_Addr    sh_addr;    /* virtual address */
  143.     Elf32_Off    sh_offset;    /* file offset */
  144.     Elf32_Word    sh_size;    /* section size */
  145.     Elf32_Word    sh_link;    /* misc info */
  146.     Elf32_Word    sh_info;    /* misc info */
  147.     Elf32_Word    sh_addralign;    /* memory alignment */
  148.     Elf32_Word    sh_entsize;    /* entry size if table */
  149. } Elf32_Shdr;
  150.  
  151. #define    SHT_NULL    0        /* sh_type */
  152. #define    SHT_PROGBITS    1
  153. #define    SHT_SYMTAB    2
  154. #define    SHT_STRTAB    3
  155. #define    SHT_RELA    4
  156. #define    SHT_HASH    5
  157. #define    SHT_DYNAMIC    6
  158. #define    SHT_NOTE    7
  159. #define    SHT_NOBITS    8
  160. #define    SHT_REL        9
  161. #define    SHT_SHLIB    10
  162. #define    SHT_DYNSYM    11
  163. #define    SHT_NUM        12
  164. #define    SHT_LOUSER    0x80000000
  165. #define    SHT_HIUSER    0xffffffff
  166.  
  167. #define    SHT_LOPROC    0x70000000    /* processor specific range */
  168. #define    SHT_HIPROC    0x7fffffff
  169.  
  170. #define    SHF_WRITE    0x1        /* sh_flags */
  171. #define    SHF_ALLOC    0x2
  172. #define    SHF_EXECINSTR    0x4
  173.  
  174. #define    SHF_MASKPROC    0xf0000000    /* processor specific values */
  175.  
  176. #define    SHN_UNDEF    0        /* special section numbers */
  177. #define SHN_TEXT        1
  178. #define SHN_DATA        2
  179. #define SHN_BSS         3
  180. #define SHN_COMMENT     4
  181. #define SHN_DEBUG       5
  182. #define SHN_LINE        6
  183. #define SHN_DATAXI      7
  184. #define SHN_DATAXC      8
  185. #define SHN_REL_TEXT    9
  186. #define SHN_REL_DATA    10
  187. #define SHN_SYMTAB      11
  188. #define SHN_STRTAB      12
  189. #define SHN_REL_DEBUG   13
  190. #define SHN_REL_LINE    14
  191. #define SHN_REL_DATAXI  15
  192. #define SHN_REL_DATAXC  16
  193. #define MAX_SCNS        17
  194. #define SHN_REL         SHN_DATAXC
  195. #define SHN_LORESERVE   0xff00
  196. #define SHN_ABS         0xfff1
  197. #define    SHN_COMMON    0xfff2
  198. #define    SHN_HIRESERVE    0xffff
  199.  
  200. #define    SHN_LOPROC    0xff00        /* processor specific range */
  201. #define    SHN_HIPROC    0xff1f
  202.  
  203. /* Symbol table */
  204.  
  205. typedef struct {
  206.     Elf32_Word    st_name;
  207.     Elf32_Addr    st_value;
  208.     Elf32_Word    st_size;
  209.     unsigned char    st_info;    /* bind, type: ELF_32_ST_... */
  210.     unsigned char    st_other;
  211.     Elf32_Half    st_shndx;    /* SHN_... */
  212. } Elf32_Sym;
  213.  
  214. #define    STN_UNDEF    0
  215.  
  216. /*    The macros compose and decompose values for S.st_info
  217.  *
  218.  *    bind = ELF32_ST_BIND(S.st_info)
  219.  *    type = ELF32_ST_TYPE(S.st_info)
  220.  *    S.st_info = ELF32_ST_INFO(bind, type)
  221.  */
  222.  
  223. #define    ELF32_ST_BIND(info)        ((info) >> 4)
  224. #define    ELF32_ST_TYPE(info)        ((info) & 0xf)
  225. #define    ELF32_ST_INFO(bind, type)    (((bind)<<4)+((type)&0xf))
  226.  
  227. #define    STB_LOCAL    0        /* BIND */
  228. #define    STB_GLOBAL    1
  229. #define    STB_WEAK    2
  230. #define    STB_NUM        3
  231.  
  232. #define    STB_LOPROC    13        /* processor specific range */
  233. #define    STB_HIPROC    15
  234.  
  235. #define    STT_NOTYPE    0        /* TYPE */
  236. #define    STT_OBJECT    1
  237. #define    STT_FUNC    2
  238. #define    STT_SECTION    3
  239. #define    STT_FILE    4
  240. #define    STT_NUM        5
  241.  
  242. #define    STT_LOPROC    13        /* processor specific range */
  243. #define    STT_HIPROC    15
  244.  
  245.  
  246. /* Relocation */
  247.  
  248. typedef struct {
  249.     Elf32_Addr    r_offset;
  250.     Elf32_Word    r_info;        /* sym, type: ELF32_R_... */
  251. } Elf32_Rel;
  252.  
  253. typedef struct {
  254.     Elf32_Addr    r_offset;
  255.     Elf32_Word    r_info;        /* sym, type: ELF32_R_... */
  256.     Elf32_Sword    r_addend;
  257. } Elf32_Rela;
  258.  
  259. /*    The macros compose and decompose values for Rel.r_info, Rela.f_info
  260.  *
  261.  *    sym = ELF32_R_SYM(R.r_info)
  262.  *    type = ELF32_R_TYPE(R.r_info)
  263.  *    R.r_info = ELF32_R_INFO(sym, type)
  264.  */
  265.  
  266. #define    ELF32_R_SYM(info)    ((info)>>8)
  267. #define    ELF32_R_TYPE(info)    ((unsigned char)(info))
  268. #define    ELF32_R_INFO(sym, type)    (((sym)<<8)+(unsigned char)(type))
  269.  
  270.  
  271. /* Note entry header */
  272.  
  273. typedef struct {
  274.     Elf32_Word    n_namesz;    /* length of note's name */
  275.     Elf32_Word    n_descsz;    /* length of note's "desc" */
  276.     Elf32_Word    n_type;        /* type of note */
  277. } Elf32_Nhdr;
  278.  
  279. /* Known values for note entry types (e_type == ET_CORE) */
  280.  
  281. #define    NT_PRSTATUS    1
  282. #define    NT_PRFPREG    2
  283. #define    NT_PRPSINFO    3
  284.  
  285.  
  286. #define    R_386_NONE        0    /* relocation type */
  287. #define    R_386_32        1
  288. #define    R_386_PC32        2
  289. #define    R_386_GOT32        3
  290. #define    R_386_PLT32        4
  291. #define    R_386_COPY        5
  292. #define    R_386_GLOB_DAT        6
  293. #define    R_386_JMP_SLOT        7
  294. #define    R_386_RELATIVE        8
  295. #define    R_386_GOTOFF        9
  296. #define    R_386_GOTPC        10
  297. #define    R_386_NUM        11
  298.  
  299. #define    ELF_386_MAXPGSZ        0x1000    /* maximum page size */
  300.  
  301. struct elf_section
  302. {
  303.   Elf32_Shdr *shdr;
  304.   unsigned char *data;
  305. };
  306.  
  307. union elf_relocation
  308. {
  309.   Elf32_Rel rel;
  310.   Elf32_Rela rela;
  311. };
  312.  
  313. #define SOURCE_NO_POS   0xffff
  314.  
  315. struct elf_lineno
  316. {
  317.   Elf32_Word    linenum;
  318.   Elf32_Half    lineposn;       // = SOURCE_NO_POS for whole line
  319.   Elf32_Word    offset;
  320. };
  321.  
  322. #pragma pack()
  323.  
  324. #ifdef  __cplusplus
  325. }
  326. #endif
  327.  
  328. #endif
  329.